﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using WorkFlowSystem.Common;
using WorkFlowSystem.DataBase;

namespace WorkFlowSystem
{
    public partial class NewsEdit : Form
    {
        public NewsEdit()
        {
            InitializeComponent();
            m_news_info = new NewsInfo();
            m_modify_mode = false;
        }

        #region 辅助方法
        private void NewsEdit_Load(object sender, EventArgs e)
        {
            if (init_modify_news() < 0)
                this.Close();
        }

        private int load_news()
        {
            Logger.WriteLog("enter load_news");
            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接");
                return -1;
            }

            //获取nid的通知信息
            string sql = "select * from news";
            DataTable dt = new DataTable();
            if (db_op.ExecuteDataTable(sql, null, false, ref dt) < 0)
            {
                Logger.WriteLog("get news info fail");
                db_op.CloseDB();
                return -1;
            }
            if (dt.Rows.Count > 0)
                m_modify_mode = true;
            m_news_info.new_id = int.Parse(dt.Rows[0]["nid"].ToString());
            m_news_info.new_title = dt.Rows[0]["n_title"].ToString();
            m_news_info.new_text = dt.Rows[0]["n_text"].ToString();
            db_op.CloseDB();
            return 0;
        }

        private int init_modify_news()
        {
            //load data from database to memory
            if (load_news() < 0)
            {
                MessageBox.Show("加载通知信息失败");
                return -1;
            }
            //init控件
            textBoxX1.Text = m_news_info.new_title;
            richTextBoxEx1.Text = m_news_info.new_text;
            return 0;
        }
        #endregion

        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX1_Click(object sender, EventArgs e)
        {
            m_news_info.new_title = textBoxX1.Text;
            m_news_info.new_text = richTextBoxEx1.Text;

            DBOperator db_op = new DBOperator();
            if (db_op.ConnectDB() < 0)
            {
                MessageBox.Show("无法连接服务器，请检查网络连接");
                return;
            }

            string sql = "";
            DataTable dt = new DataTable();
            List<SqlParameter> para_list = new List<SqlParameter>();

            if (m_modify_mode)
            {
                sql = "update news set n_title=@title, n_text=@text, n_update_uid=@update_uid, n_update_date=@update_date " +
                    "where nid=@nid";
            }
            else
            {
                sql = "insert into news(nid, n_title, n_text, n_insert_uid, n_insert_date) " +
                    "values(@title, @text, @insert_uid, @insert_date)";
            }
            para_list.Clear();
            para_list.Add(new SqlParameter("@nid", m_news_info.new_id));
            para_list.Add(new SqlParameter("@title", m_news_info.new_title));
            para_list.Add(new SqlParameter("@text", m_news_info.new_text));
            para_list.Add(new SqlParameter("@insert_uid", CurrentUser.user_id));
            para_list.Add(new SqlParameter("@insert_date", DateTime.Now));
            para_list.Add(new SqlParameter("@update_uid", CurrentUser.user_id));
            para_list.Add(new SqlParameter("@update_date", DateTime.Now));
            if (db_op.ExecuteNonQuery(sql, para_list, false) < 0)
            {
                if (m_modify_mode)
                {
                    MessageBox.Show("修改通知信息失败");
                    db_op.CloseDB();
                    return;
                }
                else
                {
                    MessageBox.Show("添加通知信息失败");
                    db_op.CloseDB();
                    return;
                }
            }
            db_op.CloseDB();
            if (OnChange != null)
                OnChange(sender, e);
            this.Close();
        }

        /// <summary>
        /// 清空
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonX2_Click(object sender, EventArgs e)
        {
            Common.CommonDefine.clear_values(this.panelEx1);
        }

        public delegate void NewsChange(object sender, EventArgs e);
        public event NewsChange OnChange;
        private NewsInfo m_news_info;
        private bool m_modify_mode;
    }

    class NewsInfo
    {
        public NewsInfo()
        {
            new_id = 0;
            new_title = "";
            new_text = "";
        }

        public int new_id;
        public string new_title;
        public string new_text;
    }
}
